package com.ysd.qxgl.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.ysd.qxgl.dto.IUsersDTO;
import com.ysd.qxgl.model.Users;
import com.ysd.qxgl.service.IUsersService;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping(path = "/users",method = RequestMethod.POST)
public class UsersController {
@Resource
private IUsersService usersService;
@ApiOperation(value = "查询所有用户")
@GetMapping("/findAll")
public Map findAll(IUsersDTO dto){
 List<Users> list= usersService.findUsers(dto);
 Map<String,Object> map=new HashMap<String,Object>();
 map.put("code", 0);
 map.put("count", usersService.getCount(dto));
 map.put("msg","查询成功");
 map.put("data",list);
 return map;
}
@ApiOperation(value = "删除用户操作")
@DeleteMapping(value = "{id}")
public Object delusers(@PathVariable String id){
    int n=usersService.del(id);
    Map<String, Object> map=new HashMap<String, Object>();
    if(n>0){
        map.put("success", "删除成功");
    }else{
        map.put("error", "删除成功");
    }
    return map;
}
@ApiOperation(value = "密码重置")
@PostMapping(value = "/reset/{id}")
public int password(@PathVariable String id){
    return usersService.password(id);
}
@ApiOperation(value = "锁定解锁操作")
@PutMapping(value = "/{islocked}/{id}")
public Object users(@PathVariable String islocked,@PathVariable String id){
    int n =usersService.islocked(id,islocked);
    Map<String,Object> map=new HashMap<String,Object>();
    if(n>0){
        map.put("success", "成功");
    }else{
        map.put("error", "失败");
    }
    return map;
}
@ApiOperation(value = "添加或修改用户",notes = "当id为空时是添加,否则是修改")
@PostMapping(value = "addOrEdit")
public void addOrEdit(@RequestBody Users user){
    usersService.addOrEdit(user);
}
@ApiOperation(value ="分配角色")
@RequestMapping("/addRoles")
 public void AddRoles(String userid,String roleid){
     usersService.addRoles(userid, roleid);
 }
 @ApiOperation(value = "移除角色")
@DeleteMapping(value = "/delRoles")
  public int delRoles(String roleid){
      return usersService.delRoles(roleid);

  }

}